package com.bank.dao;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bank.entity.Subject;
import com.bank.excption.CommonException;

@Repository(value="subjectDao")
public class SubjectDaoImpl implements SubjectDao {
	
	Logger logger = Logger.getLogger(SubjectDaoImpl.class);
	
	@Autowired
	SessionFactory sessionFactory;

	@SuppressWarnings("unchecked") 
	public Subject get(String subjectNumber) {
		logger.debug("SubjectDaoImpl.get()......");
		String sql = "from Subject where subject_number='"+subjectNumber+"'";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		List<Subject> list = query.list();
		Subject subject = null;
		if(list.size()==0)
		{
			return null;
		}
		logger.debug("list.size():"+list.size());
		for(Subject subjectFor:list){
			subject = subjectFor;
		}
		return subject;

	}

	public void save(Subject subject) throws CommonException {
		logger.debug("SubjectDaoImpl.save()......");
		Session session = sessionFactory.getCurrentSession();
		session.save(subject);
		session.flush();

	}

	public void update(Subject subject) throws CommonException {
		// TODO Auto-generated method stub
		logger.debug("SubjectDaoImpl.save()......");
		Session session = sessionFactory.getCurrentSession();
		session.update(subject);
		session.flush();
	}
	
	@SuppressWarnings("unchecked") 
	public List<Object> getSubjecList(Map<String, Object> params) {
		String sql = "from Subject where 1=1 ";
		Integer offsetInt  = (Integer)params.get("offset");
		Integer limitInt = (Integer)params.get("limit");
		String subjectNumber = (String)params.get("subject_number");
		String subjectName  = (String)params.get("subject_name");
		String subjectType = (String)params.get("subject_type");
		String superSubject = (String)params.get("super_subject");
		String subjectBalaneDirect = (String)params.get("subject_balance_direct");
		String subjectStatus = (String)params.get("subject_status");

		if(subjectNumber!=null && subjectNumber.equals("")==false){
			sql = sql+" and subject_number ='"+subjectNumber+"'";
		}
		if(subjectName != null && subjectName.equals("")==false){
			sql = sql+" and  subject_name='"+subjectName+"'";
		}
		if(subjectType != null && subjectType.equals("")==false){
			sql = sql+"  and subject_type='"+subjectType+"'";
		}
		if(superSubject != null && superSubject.equals("")==false){
			sql = sql+"  and super_subject='"+superSubject+"'";
		}

		if(subjectBalaneDirect != null && subjectBalaneDirect.equals("")==false){
			sql = sql+"  and subject_balance_direct='"+subjectBalaneDirect+"'";
		}
		if(subjectStatus != null && subjectStatus.equals("")==false){
			sql = sql+"  and subject_status='"+subjectStatus+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(offsetInt);
		query.setMaxResults(limitInt);
		List<Object> subjectList = query.list();	
		return subjectList;
	}
	
	
	public Integer getSubjectListCount(Map<String, Object> parmas) {
		String sql = "select count(*)  from Subject where 1=1 ";
		String subjectNumber = (String)parmas.get("subject_number");
		String subjectName  = (String)parmas.get("subject_name");
		String subjectType = (String)parmas.get("subject_type");
		String superSubject = (String)parmas.get("super_subject");
		String subjectBalaneDirect = (String)parmas.get("subject_balance_direct");
		String subjectStatus = (String)parmas.get("subject_status");

		if(subjectNumber!=null && subjectNumber.equals("")==false){
			sql = sql+" and subject_number ='"+subjectNumber+"'";
		}
		if(subjectName != null && subjectName.equals("")==false){
			sql = sql+" and  subject_name='"+subjectName+"'";
		}
		if(subjectType != null && subjectType.equals("")==false){
			sql = sql+"  and subject_type='"+subjectType+"'";
		}
		if(superSubject != null && superSubject.equals("")==false){
			sql = sql+"  and super_subject='"+superSubject+"'";
		}

		if(subjectBalaneDirect != null && subjectBalaneDirect.equals("")==false){
			sql = sql+"  and subject_balance_direct='"+subjectBalaneDirect+"'";
		}
		if(subjectStatus != null && subjectStatus.equals("")==false){
			sql = sql+"  and subject_status='"+subjectStatus+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		int totalRecord = Integer.parseInt(session.createQuery(sql).uniqueResult().toString());
		return totalRecord;
	}

}
